﻿
using Xacml.Core;
using Xacml.Core.Interfaces;
using System.Collections;

namespace Xacml.Core.Runtime
{
  public class BagValue : IFunctionParameter
  {
    private ArrayList _elements = new ArrayList();
    private IDataType _dataType;

    public static BagValue Empty
    {
      get
      {
        return new BagValue((IDataType) null);
      }
    }

    public bool IsBag
    {
      get
      {
        return true;
      }
    }

    public int BagSize
    {
      get
      {
        return this._elements.Count;
      }
    }

    public ArrayList Elements
    {
      get
      {
        return this._elements;
      }
    }

    public BagValue(IDataType dataType)
    {
      this._dataType = dataType;
    }

    public void Add(object value)
    {
      this._elements.Add(value);
    }

    public IDataType GetType(EvaluationContext context)
    {
      return this._dataType;
    }

    public object GetTypedValue(IDataType dataType, int parNo)
    {
      if (dataType != DataTypeDescriptor.Bag)
        throw new EvaluationException("invalid datatype.");
      return (object) this;
    }

    public IFunction GetFunction(int parNo)
    {
      throw new EvaluationException(Resource.ResourceManager[Resource.MessageKey.exc_invalid_datatype_in_stringvalue, new object[2]
      {
        (object) parNo,
        (object) "BagValue"
      }]);
    }
  }
}
